import { useAppStore } from '@/store';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import { en, zhCn } from 'element-plus/es/locales.mjs';
import { createApp } from 'vue';
import App from './App.vue';
import i18n from './locales';
import router from './router';
import './scss/index.scss';
import pinia from './store';

const app = createApp(App);

// 先注册 pinia
app.use(pinia);

// 获取 store 实例
const appStore = useAppStore();

// 初始化主题
document.documentElement.style.setProperty('--el-color-primary', appStore.primaryColor);
if (appStore.isDark) {
  document.documentElement.classList.add('dark');
  document.documentElement.style.setProperty('--el-text-color-regular', '#303133');
}

// 设置语言
i18n.global.locale.value = appStore.language;

// 注册 Element Plus 图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

app.use(router);
app.use(i18n);
app.use(ElementPlus, {
  locale: i18n.global.locale.value === 'zh-CN' ? zhCn : en,
  size: 'default' as const,
  zIndex: 3000
});

app.mount('#app');
